#include "net/tcpserver.h"
#include "net/connect.h"
#include "base/timer.h"
#include "base/eventloop.h"
#include "util/mutex.h"
#include "base/log.h"
#include "base/logthread.h"
#include "MyRpc/message.h"
#include "MyRpc/rpc.h"
#include "MyRpc/rpccall.hpp"

myrpc::Message *message = nullptr;
static int count = 0;
void close_func(net_tools::net::Connect *connect)
{
  connect->Print_ip_port("close");
};
char buf[1500] = {};
void read_func(net_tools::net::Connect *connect)
{
  int size = connect->Read(buf, 1500);
  connect->Write(buf, size);
  ++count;
};
void conn_func(net_tools::net::Connect *connect)
{
  connect->Print_ip_port("connect");
  connect->Set_read_callback(read_func);
  connect->Set_close_callback(close_func);
  connect->Write(buf, 1500);
};

int main(int argc, char *argv[])
{
  net_tools::Config config("Server");
  net_tools::Logthread log;
  message = myrpc::Message::Get_singleton();
  net_tools::Tcpserver tcp;
  tcp.Add_timejob_after_sec(
      "show count", []()
      { NT_LOG_INFO << count << NT_LOG_ENDL; },
      0, 1, -1);
  tcp.Set_connect_over_callback(conn_func);
  tcp.Initserver(1, 0);
  tcp.Startserver();
};